package org.jeecg.modules.iot.aio;

import com.alibaba.fastjson.JSON;
import org.jeecg.modules.iot.aio.message.CheckMessage;
import org.jeecg.modules.iot.aio.task.QueryMessageRunnable;
import org.jeecg.modules.iot.aio.task.SendMessageRunnable;
import org.jeecg.modules.iot.aliyun.DeviceOnline;
import org.jeecg.modules.iot.aliyun.MessageBody;
import org.jeecg.modules.iot.util.IotClientUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.utils.Threads;
import org.tio.utils.lock.SetWithLock;
import java.util.HashSet;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 这里是一个注释
 *
 * @author scw
 * @version v1.0
 * @date
 */
public class AioConfiguration {

    private static Logger log = LoggerFactory.getLogger(AioConfiguration.class);
    private final static AtomicInteger ID_ATOMIC = new AtomicInteger();
    public QueueList queueList = new QueueList();
    private String productKey;
    private IotClientUtil iotClientUtil;
    public SendMessageRunnable sendMessageRunnable = null;
    private Exception exception;
    public QueryMessageRunnable queryMessageRunnable = null;
    /**
     * 开启一个线程池
     */
    public ThreadPoolExecutor aioExecutor = null;

    public SetWithLock<ChannelContext> connections = new SetWithLock<ChannelContext>(new HashSet<ChannelContext>());

    public AioConfiguration(String productKey ,IotClientUtil iotClientUtil){
        this.aioExecutor = Threads.getGroupExecutor();
        this.productKey = productKey;
        this.iotClientUtil = iotClientUtil;
        this.queryMessageRunnable = new QueryMessageRunnable(this.aioExecutor,this);
        /**检测*/
    }

    public String getProductKey() {
        return productKey;
    }

    /**
     * 针对不同设备的信息添加到不同设备的ChannelContext中
     * @param messageBody
     */
    public void handlerMessage(MessageBody messageBody) {
        String message = JSON.toJSONString(messageBody);
        log.info("收到消息：{}",message);
        String deviceSn = messageBody.getDeviceName();
        ChannelContext channelContext = this.queueList.getChannelContext(deviceSn,this);
        channelContext.addMNSMessage(messageBody);
    }


    private void sendPopupWait(String rentboxSN, String message) throws Exception {
        this.checkOnlineStatus(rentboxSN);
        CheckMessage messages = new CheckMessage();
        messages.setMessage(message);
        messages.setSn(rentboxSN);
        messages.setProductKey(this.productKey);
        /**
         * check结束后
         */
//        checkMessageRunnable.getMsgQueue().add(messages);
//        checkMessageRunnable.execute();
    }

    public void checkOnlineStatus(String rentboxSN) throws Exception {
        DeviceOnline onlineStatus = iotClientUtil.getDeviceStatus(this.productKey, rentboxSN);
        if(!onlineStatus.name().equals("ONLINE")){
            throw new Exception("Device is Offline");
        }
    }

    public IotClientUtil getIotClientUtil() {
        return iotClientUtil;
    }

    public Exception getException() {
        return exception;
    }

    public void setException(Exception exception) {
        this.exception = exception;
    }
}
